home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / jrh-rkrm-partone / exec_library / deviceio / deviceuse.e < prev   
Text File  |  1995-03-31  |  4KB  |  86 lines

  1. -> DeviceUse.e - an example of using an Amiga device (here, serial device)
  2. ->    - attempt to create a message port with createPort()   (from amigalib)
  3. ->    - attempt to create the I/O request with ereateExtIO() (from amigalib)
  4. ->    - attempt to open the serial device with Exec OpenDevice()
  5. ->
  6. -> If successful, use the serial command SDCMD_QUERY, then reverse our steps.
  7. -> If we encounter an error at any time, we will gracefully exit.  Note that
  8. -> applications which require at least V37 OS should use the Exec functions
  9. -> CreateMsgPort()/DeleteMsgPort() and CreateIORequest()/DeleteIORequest()
  10. -> instead of the similar amigalib functions which are used in this example.
  11.  
  12. OPT PREPROCESS
  13.  
  14. MODULE 'amigalib/io',
  15.        'amigalib/ports',
  16.        'devices/serial',
  17.        'exec/io',
  18.        'exec/ports'
  19.  
  20. ENUM ERR_NONE, ERR_DEV, ERR_IO, ERR_PORT
  21.  
  22. RAISE ERR_DEV IF OpenDevice()<>0
  23.  
  24. PROC main() HANDLE
  25.   DEF serialMP=NIL:PTR TO mp, serialIO=NIL:PTR TO ioextser,
  26.       reply:PTR TO ioextser
  27.   -> Create the message port.
  28.   IF NIL=(serialMP:=createPort(NIL,NIL)) THEN Raise(ERR_PORT)
  29.  
  30.   -> Create the I/O request.  Note that 'devices/serial' defines the type of
  31.   -> io required by the serial device--an ioextser.  Many devices require
  32.   -> specialised extended IO requests which start with an embedded io object.
  33.   -> E-Note: ignore the rubbish about casting
  34.   IF NIL=(serialIO:=createExtIO(serialMP, SIZEOF ioextser)) THEN Raise(ERR_IO)
  35.  
  36.   -> Open the serial device (non-zero return value means failure here).
  37.   OpenDevice(SERIALNAME, 0, serialIO, 0)
  38.  
  39.   -> Device is open
  40.   serialIO.iostd.command:=SDCMD_QUERY
  41.   -> DoIO - demonstrates synchronous device use, returns error or 0.
  42.   IF DoIO(serialIO)
  43.     WriteF('Query failed.  Error - \d\n', serialIO.iostd.error)
  44.   ELSE
  45.     -> Print serial device status - see include file for meaning.
  46.     -> Note that with DoIO, the Wait and GetMsg are done by Exec.
  47.     WriteF('Serial device status: $\h\n\n', serialIO.status)
  48.   ENDIF
  49.  
  50.   serialIO.iostd.command:=SDCMD_QUERY
  51.   -> SendIO - demonstrates asynchronous device use (returns immediately).
  52.   SendIO(serialIO)
  53.   -> We could do other things here while the query is being done.  And to
  54.   -> manage our asynchronous device IO:
  55.   ->   - we can CheckIO(serialIO) to check for completion
  56.   ->   - we can AbortIO(serialIO) to abort the command
  57.   ->   - we can WaitPort(serialMP) to wait for any serial port reply
  58.   ->  OR we can WaitIO(serialIO) to wait for this specific IO request
  59.   ->  OR we can Wait(Shl(1, serialMP.sigbit)) for reply port signal
  60.   Wait(Shl(1, serialMP.sigbit))
  61.  
  62.   WHILE reply:=GetMsg(serialMP)
  63.     -> Since we sent out only one serialIO request the while loop is not
  64.     -> really needed--we only expect one reply to our one query command, and
  65.     -> the reply message pointer returned by GetMsg() will just be another
  66.     -> pointer to our one serialIO request.  With Wait() or WaitPort(), you
  67.     -> must GetMsg() the message.
  68.     IF reply.iostd.error
  69.       WriteF('Query failed.  Error - \d\n', reply.iostd.error)
  70.     ELSE
  71.       WriteF('Serial device status: $\h\n\n', reply.status)
  72.     ENDIF
  73.   ENDWHILE
  74.   CloseDevice(serialIO)  -> Close the serial device.
  75. EXCEPT DO
  76.   IF serialIO THEN deleteExtIO(serialIO)  -> Delete the I/O request.
  77.   IF serialMP THEN deletePort(serialMP)   -> Delete the message port.
  78.   SELECT exception
  79.   CASE ERR_DEV;  WriteF('Error: \s did not open\n', SERIALNAME)
  80.   -> Inform user that the I/O request could be created.
  81.   CASE ERR_IO;   WriteF('Error: Could not create I/O request\n')
  82.   -> Inform user that the message port could not be created.
  83.   CASE ERR_PORT; WriteF('Error: Could not create message port\n')
  84.   ENDSELECT
  85. ENDPROC
  86.